﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Edit_F4CheckRV201F06 : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        private Dictionary<string, string> required_list;

        private DataSet ds;
        private DataViewManager dsView;

        public string serial;

        public frm_Edit_F4CheckRV201F06()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.ds = new DataSet("RobotOrderDetail");

            this.required_list = new Dictionary<string, string>() { { "serialNo", "Serial No" }, { "writtenBy", "Written By" },
            { "serial_No_RJ", "Serial No" }, { "checkBy", "Check By"}, {"writtenDate", "Written Date"}, {"exportDate", "Export Date"},
            {"customerID", "Customer"}, {"appDiagram", "Appearance Diagram"}, {"wiringDiagram", "Wiring Diagram"},
            {"RC_510_Serial", "RC-510 Serial"}, {"SB020_Serial", "SB020 Serial"}, {"SB017A_Serial", "SB017A Serial"},
            {"Indicator1_Serial", "Indicator1 Serial"}, {"SB019_Door_Serial", "SB019(Door) Serial"},
            {"SB019_Stg1_Serial", "SB019(Stg1) Serial"}, {"SB019_Stg2_Serial", "SB019(Stg2) Serial"},
            {"Y_Axis_Driver_Serial", "Y Axis Driver Serial"}, {"Z_Axis_Driver_Serial", "Z Axis Driver Serial"},
            {"CID_Serial", "CID Serial"}, {"RC_510_Program_File", "RC-510 Program File"}, {"Program_Version", "Program Version"},
            {"FPGA_Version", "FPGA Version"}, {"Initialize_Data_File", "Initialize Data File"}, {"Body_No", "Body No"},
            {"IP_Address", "IP Address"},
            {"MAC_Address1", "MAC Address 1"}, {"MAC_Address2", "MAC Address 1"}, {"MAC_Address3", "MAC Address 1"},
            {"Y_Position1", "Y Axis Position1"}, {"Y_Position2", "Y Axis Position2"}, {"Y_Position3", "Y Axis Position3"},
            {"Y_Position4", "Y Axis Position4"}, {"Y_Position5", "Y Axis Position5"}, {"Y_Position6", "Y Axis Position6"},
            {"Z_Position1", "Z Axis Position1"}, {"Z_Position2", "Z Axis Position2"}, {"Z_Position3", "Z Axis Position3"},
            {"Z_Position4", "Z Axis Position4"}, {"Z_Position5", "Z Axis Position5"}, {"Z_Position6", "Z Axis Position6"},
            {"Mapping_Top", "Mapping Top"}, {"Mapping_Bottom", "Mapping Bottom"}, {"BRC_Reading", "BRC Reading"},
            {"Stacker_Hand_Threshold", "Stacker Hand Threshold"}, {"Laser_Print_Threshold", "Laser Print Threshold"},
            {"YAX1_Driver_Ver", "YAX1 Driver Ver"}, {"ZAX1_Driver_Ver", "ZAX1 Driver Ver"}, {"RIO_Door_Ver1", "RIO/Door Ver1"},
            {"RIO_Stage_Ver1", "RIO/Stage Ver1"}, {"RIO_Stage_Ver2", "RIO/Stage Ver2"}, {"RIO_Indicator1_Ver1", "RIO/Indicator1 Ver1"},
            {"RIO_Indicator1_Ver2", "RIO/Indicator1 Ver2"}, {"CPLD0_Ver", "CPLD0 Ver"}, {"CPLD1_Ver", "CPLD1 Ver"},
            {"LoaderLD_510_Ver", "Loader LD-510 Ver"}};
                        
            LoadCustomerList();
            LoadStaffList();
            LoadDefaultData();            
        }

        ////////////////////////////////////////////////////
        /// Load Serial List
        ////////////////////////////////////////////////////
        private void LoadSerialList()
        {

            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [SerialNo], [CompID] FROM [231_RobotOrderDetail]"
                    + " WHERE(SerialNo LIKE N'E%') AND SerialNo NOT IN"
                    + " (SELECT [Serial No(RRC)] COLLATE Vietnamese_CI_AS FROM [231_RV201F06] WHERE [Serial No(RRC)] <> '" + this.serial + "')";
                SqlDataAdapter da1 = new SqlDataAdapter();
                da1.SelectCommand = cmd_sql;
                da1.TableMappings.Add("Table", "RobotOrderDetail");
                da1.Fill(ds);
                dsView = ds.DefaultViewManager;
                cmd_sql.Dispose();
                myConnection.Close();

                this.serialNo.DataSource = dsView;
                this.serialNo.DisplayMember = "RobotOrderDetail.SerialNo";
                this.serialNo.ValueMember = "RobotOrderDetail.SerialNo";
                this.serialNo.AutoCompleteSource = AutoCompleteSource.ListItems;

                this.partNo.DataBindings.Add("Text", dsView, "RobotOrderDetail.CompID");
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        ////////////////////////////////////////////////////
        /// Load Customer List
        ////////////////////////////////////////////////////
        private void LoadCustomerList()
        {

            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [CustomerID] FROM [231_Customer]";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["CustomerID"].ToString().Trim();
                    item.Text = dataReader["CustomerID"].ToString().Trim();
                    this.customerID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Load Staff List
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadStaffList()
        {
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT StaffID, StaffName FROM [222_Staff] WHERE StopDate IS NULL ORDER BY StaffID";
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    string[] fullname = dataReader["StaffName"].ToString().Trim().Split(' ');

                    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
                    TextInfo textInfo = cultureInfo.TextInfo;
                    string txt = textInfo.ToTitleCase(fullname[fullname.Length - 1].ToLower());

                    item.Text = txt + dataReader["StaffID"].ToString().Trim();                    
                    item.Value = txt + dataReader["StaffID"].ToString().Trim();

                    this.writtenBy.Properties.Items.Add(item);
                    this.checkBy.Properties.Items.Add(item);
                }
                dataReader.Close();
                sql_cmd.Dispose();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        ////////////////////////////////////////////////////
        /// Load Default Data
        ////////////////////////////////////////////////////
        private void LoadDefaultData()
        {
            
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [Appearance Diagram], [Wiring Diagram], [RC-510 Program File],"
                    + " [Program Version], [FPGA Version], [Initialize Data File], [IP Adress], [YAX1 Driver Ver],"
                    + " [ZAX1 Driver Ver], [RIO/Door Ver 1], [RIO/Stage Ver 1], [RIO/Stage Ver 2],"
                    + " [RIO/Indicator1 Ver 1], [RIO/Indicator1 Ver 2], [CPLD0 Ver], [CPLD1 Ver], [Loader LD-510 Ver]"
                    + " FROM [231_RV201F06] GROUP BY [Appearance Diagram], [Wiring Diagram], [RC-510 Program File],"
                    + " [Program Version], [FPGA Version], [Initialize Data File], [IP Adress], [YAX1 Driver Ver],"
                    + " [ZAX1 Driver Ver], [RIO/Door Ver 1], [RIO/Stage Ver 1], [RIO/Stage Ver 2], [RIO/Indicator1 Ver 1],"
                    + " [RIO/Indicator1 Ver 2], [CPLD0 Ver], [CPLD1 Ver], [Loader LD-510 Ver]";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item1 = new ComboboxItem();
                    item1.Value = dataReader["Appearance Diagram"].ToString().Trim();
                    item1.Text = dataReader["Appearance Diagram"].ToString().Trim();
                    this.appDiagram.Properties.Items.Add(item1);

                    ComboboxItem item2 = new ComboboxItem();
                    item2.Value = dataReader["Wiring Diagram"].ToString().Trim();
                    item2.Text = dataReader["Wiring Diagram"].ToString().Trim();
                    this.wiringDiagram.Properties.Items.Add(item2);

                    ComboboxItem item3 = new ComboboxItem();
                    item3.Value = dataReader["RC-510 Program File"].ToString().Trim();
                    item3.Text = dataReader["RC-510 Program File"].ToString().Trim();
                    this.RC_510_Program_File.Properties.Items.Add(item3);

                    ComboboxItem item4 = new ComboboxItem();
                    item4.Value = dataReader["Program Version"].ToString().Trim();
                    item4.Text = dataReader["Program Version"].ToString().Trim();
                    this.Program_Version.Properties.Items.Add(item4);

                    ComboboxItem item5 = new ComboboxItem();
                    item5.Value = dataReader["FPGA Version"].ToString().Trim();
                    item5.Text = dataReader["FPGA Version"].ToString().Trim();
                    this.FPGA_Version.Properties.Items.Add(item5);

                    ComboboxItem item6 = new ComboboxItem();
                    item6.Value = dataReader["Initialize Data File"].ToString().Trim();
                    item6.Text = dataReader["Initialize Data File"].ToString().Trim();
                    this.Initialize_Data_File.Properties.Items.Add(item6);

                    ComboboxItem item7 = new ComboboxItem();
                    item7.Value = dataReader["IP Adress"].ToString().Trim();
                    item7.Text = dataReader["IP Adress"].ToString().Trim();
                    this.IP_Address.Properties.Items.Add(item7);

                    ComboboxItem item8 = new ComboboxItem();
                    item8.Value = dataReader["YAX1 Driver Ver"].ToString().Trim();
                    item8.Text = dataReader["YAX1 Driver Ver"].ToString().Trim();
                    this.YAX1_Driver_Ver.Properties.Items.Add(item8);

                    ComboboxItem item9 = new ComboboxItem();
                    item9.Value = dataReader["ZAX1 Driver Ver"].ToString().Trim();
                    item9.Text = dataReader["ZAX1 Driver Ver"].ToString().Trim();
                    this.ZAX1_Driver_Ver.Properties.Items.Add(item9);

                    ComboboxItem item10 = new ComboboxItem();
                    item10.Value = dataReader["RIO/Door Ver 1"].ToString().Trim();
                    item10.Text = dataReader["RIO/Door Ver 1"].ToString().Trim();
                    this.RIO_Door_Ver1.Properties.Items.Add(item10);

                    ComboboxItem item11 = new ComboboxItem();
                    item11.Value = dataReader["RIO/Stage Ver 1"].ToString().Trim();
                    item11.Text = dataReader["RIO/Stage Ver 1"].ToString().Trim();
                    this.RIO_Stage_Ver1.Properties.Items.Add(item11);

                    ComboboxItem item12 = new ComboboxItem();
                    item12.Value = dataReader["RIO/Stage Ver 2"].ToString().Trim();
                    item12.Text = dataReader["RIO/Stage Ver 2"].ToString().Trim();
                    this.RIO_Stage_Ver2.Properties.Items.Add(item12);

                    ComboboxItem item13 = new ComboboxItem();
                    item13.Value = dataReader["RIO/Indicator1 Ver 1"].ToString().Trim();
                    item13.Text = dataReader["RIO/Indicator1 Ver 1"].ToString().Trim();
                    this.RIO_Indicator1_Ver1.Properties.Items.Add(item13);

                    ComboboxItem item14 = new ComboboxItem();
                    item14.Value = dataReader["CPLD0 Ver"].ToString().Trim();
                    item14.Text = dataReader["CPLD0 Ver"].ToString().Trim();
                    this.CPLD0_Ver.Properties.Items.Add(item14);

                    ComboboxItem item15 = new ComboboxItem();
                    item15.Value = dataReader["CPLD1 Ver"].ToString().Trim();
                    item15.Text = dataReader["CPLD1 Ver"].ToString().Trim();
                    this.CPLD1_Ver.Properties.Items.Add(item15);

                    ComboboxItem item16 = new ComboboxItem();
                    item16.Value = dataReader["RIO/Indicator1 Ver 2"].ToString().Trim();
                    item16.Text = dataReader["RIO/Indicator1 Ver 2"].ToString().Trim();
                    this.RIO_Indicator1_Ver2.Properties.Items.Add(item16);

                    ComboboxItem item17 = new ComboboxItem();
                    item17.Value = dataReader["Loader LD-510 Ver"].ToString().Trim();
                    item17.Text = dataReader["Loader LD-510 Ver"].ToString().Trim();
                    this.LoaderLD_510_Ver.Properties.Items.Add(item17);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();

                myConnection.Open();
                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [SerialNo] FROM [231_F5Detail]"
                    + " WHERE ([FinishDate] > DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) - 500)";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["SerialNo"].ToString().Trim();
                    item.Text = dataReader["SerialNo"].ToString().Trim();

                    this.RC_510_Serial.Properties.Items.Add(item);
                    this.SB020_Serial.Properties.Items.Add(item);
                    this.SB017A_Serial.Properties.Items.Add(item);
                    this.Indicator1_Serial.Properties.Items.Add(item);                    
                    this.SB019_Door_Serial.Properties.Items.Add(item);
                    this.SB019_Stg1_Serial.Properties.Items.Add(item);                    
                    this.SB019_Stg2_Serial.Properties.Items.Add(item);
                    this.Y_Axis_Driver_Serial.Properties.Items.Add(item);
                    this.Z_Axis_Driver_Serial.Properties.Items.Add(item);                    
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            } 
        }

        /// <summary>
        /// Load Data to Edit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public bool LoadData()
        {
            bool ret = true;
            if (string.IsNullOrEmpty(this.serial))
            {
                ret = false;
            }
            else
            {
                LoadSerialList();
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "SELECT t1.*, t2.[CompID]"
                        + " FROM [231_RV201F06] AS t1 LEFT JOIN"
                        + " [231_RobotOrderDetail] AS t2 ON t1.[Serial No(RRC)] COLLATE Vietnamese_CI_AS=t2.[SerialNo]"
                        + " WHERE t1.[Serial No(RRC)]='" + this.serial + "'";
                    SqlDataReader dataReader = sql_cmd.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.serialNo.SelectedIndex = this.serialNo.FindString(serial);
                        //this.serialNo.Text = serial;
                        //this.serial_No_RJ.Text = dataReader["Serial No(RJ)"].ToString().Trim();
                        //this.partNo.Text = dataReader["Comp No#"].ToString().Trim();
                        
                        for (int i = 0; i < this.customerID.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.customerID.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Customer"].ToString().Trim())
                            {
                                this.customerID.SelectedIndex = i;
                            }
                        }

                        string written = dataReader["Written(Name)"].ToString().Trim()
                            + dataReader["Written(No#)"].ToString().Trim();
                        for (int i = 0; i < this.writtenBy.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.writtenBy.Properties.Items[i];
                            if (item.Value.ToString().ToUpper() == written.ToUpper())
                            {
                                this.writtenBy.SelectedIndex = i;
                            }
                        }
                        //this.writtenBy.Text = dataReader["Written(Name)"].ToString().Trim() 
                        //    + dataReader["Written(No#)"].ToString().Trim();
                        //this.writtenName.Text = dataReader["Written(Name)"].ToString().Trim();
                        //this.writtenID.Text = dataReader["Written(No#)"].ToString().Trim();
                        string check = dataReader["Checked(Name)"].ToString().Trim()
                            + dataReader["Checked(No#)"].ToString().Trim();
                        for (int i = 0; i < this.checkBy.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.checkBy.Properties.Items[i];
                            if (item.Value.ToString().ToUpper() == check.ToUpper())
                            {
                                this.checkBy.SelectedIndex = i;
                            }
                        }
                        //this.checkBy.Text = dataReader["Checked(Name)"].ToString().Trim()
                        //    + dataReader["Checked(No#)"].ToString().Trim();
                        //this.checkName.Text = dataReader["Checked(Name)"].ToString().Trim();
                        //this.checkID.Text = dataReader["Checked(No#)"].ToString().Trim();
                        string w_date = dataReader["Written Day"].ToString().Trim() + "/"
                            + dataReader["Written Month"].ToString().Trim() + "/"
                            + dataReader["Written Year"].ToString().Trim(); 
                        this.writtenDate.EditValue = DateTime.ParseExact(w_date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                        string e_date = dataReader["Export Day"].ToString().Trim() + "/"
                            + dataReader["Export Month"].ToString().Trim() + "/"
                            + dataReader["Export Year"].ToString().Trim();
                        this.exportDate.EditValue = DateTime.ParseExact(e_date, "dd/MM/yyyy", CultureInfo.InvariantCulture);

                        for (int i = 0; i < this.appDiagram.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.appDiagram.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Appearance Diagram"].ToString().Trim())
                            {
                                this.appDiagram.SelectedIndex = i;
                            }
                        }
                        for (int i = 0; i < this.wiringDiagram.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.wiringDiagram.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Wiring Diagram"].ToString().Trim())
                            {
                                this.wiringDiagram.SelectedIndex = i;
                            }
                        }
                                                
                        for (int i = 0; i < this.RC_510_Serial.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.RC_510_Serial.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RC-510 Serial"].ToString().Trim())
                            {
                                this.RC_510_Serial.SelectedIndex = i;
                            }
                        }
                        
                        for (int i = 0; i < this.SB020_Serial.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.SB020_Serial.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["SB020 Serial"].ToString().Trim())
                            {
                                this.SB020_Serial.SelectedIndex = i;
                            }
                        }
                        

                        for (int i = 0; i < this.SB017A_Serial.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.SB017A_Serial.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["SB017A Serial"].ToString().Trim())
                            {
                                this.SB017A_Serial.SelectedIndex = i;
                            }
                        }
                        

                        for (int i = 0; i < this.Indicator1_Serial.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.Indicator1_Serial.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Indicator1 Serial"].ToString().Trim())
                            {
                                this.Indicator1_Serial.SelectedIndex = i;
                            }
                        }
                        

                        for (int i = 0; i < this.SB019_Door_Serial.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.SB019_Door_Serial.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["SB019(Door) Serial"].ToString().Trim())
                            {
                                this.SB019_Door_Serial.SelectedIndex = i;
                            }
                        }
                        

                        for (int i = 0; i < this.SB019_Stg1_Serial.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.SB019_Stg1_Serial.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["SB019(Stg1) Serial"].ToString().Trim())
                            {
                                this.SB019_Stg1_Serial.SelectedIndex = i;
                            }
                        }
                        

                        for (int i = 0; i < this.SB019_Stg2_Serial.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.SB019_Stg2_Serial.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["SB019(Stg2) Serial"].ToString().Trim())
                            {
                                this.SB019_Stg2_Serial.SelectedIndex = i;
                            }
                        }
                        
                        for (int i = 0; i < this.Y_Axis_Driver_Serial.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.Y_Axis_Driver_Serial.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Y-Axis Driver Serial"].ToString().Trim())
                            {
                                this.Y_Axis_Driver_Serial.SelectedIndex = i;
                            }
                        }
                        
                        for (int i = 0; i < this.Z_Axis_Driver_Serial.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.Z_Axis_Driver_Serial.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Z-Axis Driver Serial"].ToString().Trim())
                            {
                                this.Z_Axis_Driver_Serial.SelectedIndex = i;
                            }
                        }

                        this.CID_Serial.Text = dataReader["CID Serial"].ToString().Trim();

                        for (int i = 0; i < this.RC_510_Program_File.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.RC_510_Program_File.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RC-510 Program File"].ToString().Trim())
                            {
                                this.RC_510_Program_File.SelectedIndex = i;
                            }
                        }

                        for (int i = 0; i < this.Program_Version.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.Program_Version.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Program Version"].ToString().Trim())
                            {
                                this.Program_Version.SelectedIndex = i;
                            }
                        }

                        for (int i = 0; i < this.FPGA_Version.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.FPGA_Version.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["FPGA Version"].ToString().Trim())
                            {
                                this.FPGA_Version.SelectedIndex = i;
                            }
                        }

                        for (int i = 0; i < this.Initialize_Data_File.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.Initialize_Data_File.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Initialize Data File"].ToString().Trim())
                            {
                                this.Initialize_Data_File.SelectedIndex = i;
                            }
                        }

                        this.Body_No.Text = dataReader["Body No"].ToString().Trim();

                        for (int i = 0; i < this.IP_Address.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.IP_Address.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["IP Adress"].ToString().Trim())
                            {
                                this.IP_Address.SelectedIndex = i;
                            }
                        }

                        this.MAC_Address1.Text = dataReader["MAC Address 1"].ToString().Trim();
                        this.MAC_Address2.Text = dataReader["MAC Address 2"].ToString().Trim();
                        this.MAC_Address3.Text = dataReader["MAC Address 3"].ToString().Trim();

                        this.Y_Position1.Text = dataReader["Y-Axis Position 1"].ToString().Trim();
                        this.Y_Position2.Text = dataReader["Y-Axis Position 2"].ToString().Trim();
                        this.Y_Position3.Text = dataReader["Y-Axis Position 3"].ToString().Trim();
                        this.Y_Position4.Text = dataReader["Y-Axis Position 4"].ToString().Trim();
                        this.Y_Position5.Text = dataReader["Y-Axis Position 5"].ToString().Trim();
                        this.Y_Position6.Text = dataReader["Y-Axis Position 6"].ToString().Trim();

                        this.Z_Position1.Text = dataReader["Z-Axis Position 1"].ToString().Trim();
                        this.Z_Position2.Text = dataReader["Z-Axis Position 2"].ToString().Trim();
                        this.Z_Position3.Text = dataReader["Z-Axis Position 3"].ToString().Trim();
                        this.Z_Position4.Text = dataReader["Z-Axis Position 4"].ToString().Trim();
                        this.Z_Position5.Text = dataReader["Z-Axis Position 5"].ToString().Trim();
                        this.Z_Position6.Text = dataReader["Z-Axis Position 6"].ToString().Trim();

                        this.Mapping_Top.Text = dataReader["Mapping Top"].ToString().Trim();
                        this.Mapping_Bottom.Text = dataReader["Mapping Bottom"].ToString().Trim();
                        this.BRC_Reading.Text = dataReader["BCR Reading"].ToString().Trim();
                        this.Stacker_Hand_Threshold.Text = dataReader["Stacker Hand Threshold"].ToString().Trim();
                        this.Laser_Print_Threshold.Text = dataReader["Laser Prt# Threshold"].ToString().Trim();

                        for (int i = 0; i < this.YAX1_Driver_Ver.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.YAX1_Driver_Ver.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["YAX1 Driver Ver"].ToString().Trim())
                            {
                                this.YAX1_Driver_Ver.SelectedIndex = i;
                            }
                        }

                        for (int i = 0; i < this.ZAX1_Driver_Ver.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.ZAX1_Driver_Ver.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["ZAX1 Driver Ver"].ToString().Trim())
                            {
                                this.ZAX1_Driver_Ver.SelectedIndex = i;
                            }
                        }

                        for (int i = 0; i < this.RIO_Door_Ver1.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.RIO_Door_Ver1.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RIO/Door Ver 1"].ToString().Trim())
                            {
                                this.RIO_Door_Ver1.SelectedIndex = i;
                            }
                        }

                        for (int i = 0; i < this.RIO_Stage_Ver1.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.RIO_Stage_Ver1.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RIO/Stage Ver 1"].ToString().Trim())
                            {
                                this.RIO_Stage_Ver1.SelectedIndex = i;
                            }
                        }

                        for (int i = 0; i < this.RIO_Stage_Ver2.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.RIO_Stage_Ver2.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RIO/Stage Ver 2"].ToString().Trim())
                            {
                                this.RIO_Stage_Ver2.SelectedIndex = i;
                            }
                        }

                        for (int i = 0; i < this.RIO_Indicator1_Ver1.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.RIO_Indicator1_Ver1.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RIO/Indicator1 Ver 1"].ToString().Trim())
                            {
                                this.RIO_Indicator1_Ver1.SelectedIndex = i;
                            }
                        }

                        for (int i = 0; i < this.RIO_Indicator1_Ver2.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.RIO_Indicator1_Ver2.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RIO/Indicator1 Ver 2"].ToString().Trim())
                            {
                                this.RIO_Indicator1_Ver2.SelectedIndex = i;
                            }
                        }

                        for (int i = 0; i < this.CPLD0_Ver.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.CPLD0_Ver.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["CPLD0 Ver"].ToString().Trim())
                            {
                                this.CPLD0_Ver.SelectedIndex = i;
                            }
                        }

                        for (int i = 0; i < this.CPLD1_Ver.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.CPLD1_Ver.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["CPLD1 Ver"].ToString().Trim())
                            {
                                this.CPLD1_Ver.SelectedIndex = i;
                            }
                        }

                        for (int i = 0; i < this.LoaderLD_510_Ver.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.LoaderLD_510_Ver.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Loader LD-510 Ver"].ToString().Trim())
                            {
                                this.LoaderLD_510_Ver.SelectedIndex = i;
                            }
                        }
                        this.Remark.Text = dataReader["Remark"].ToString().Trim();
                    }
                    sql_cmd.Dispose();
                    myConnection.Close();
                }
                catch (Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show(ex.Message);
                    ret = false;
                }
            }
            return ret;
        }
        
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        
        ///////////////////////////
        /// When Click Add Button
        ///////////////////////////        
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string error = null;

            foreach (Control c in this.Controls)
            {
                if (c is System.Windows.Forms.ComboBox)
                {
                    if (((System.Windows.Forms.ComboBox)c).SelectedIndex < 0)
                    {
                        if (required_list.ContainsKey(((System.Windows.Forms.ComboBox)c).Name))
                        {
                            error = "Please select " + required_list[((System.Windows.Forms.ComboBox)c).Name];
                        }
                    }
                }

                if (c is DevExpress.XtraEditors.ComboBoxEdit)
                {
                    if (((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex < 0)
                    {
                        if (required_list.ContainsKey(((DevExpress.XtraEditors.ComboBoxEdit)c).Name))
                        {
                            error = "Please select " + required_list[((DevExpress.XtraEditors.ComboBoxEdit)c).Name];
                        }
                    }
                }

                if (c is DevExpress.XtraEditors.TextEdit)
                {
                    if (string.IsNullOrEmpty(((DevExpress.XtraEditors.TextEdit)c).Text.Trim()))
                    {
                        if (required_list.ContainsKey(((DevExpress.XtraEditors.TextEdit)c).Name))
                        {
                            error = "Please enter " + required_list[((DevExpress.XtraEditors.TextEdit)c).Name];
                        }
                    }
                }

                if (c is System.Windows.Forms.TextBox)
                {
                    if (string.IsNullOrEmpty(((System.Windows.Forms.TextBox)c).Text.Trim()))
                    {
                        if (required_list.ContainsKey(((System.Windows.Forms.TextBox)c).Name))
                        {
                            error = "Please enter " + required_list[((System.Windows.Forms.TextBox)c).Name];
                        }
                    }
                }

                if (c is System.Windows.Forms.GroupBox)
                {
                    foreach (Control sub_c in c.Controls)
                    {
                        if (sub_c is DevExpress.XtraEditors.TextEdit)
                        {
                            if (string.IsNullOrEmpty(((DevExpress.XtraEditors.TextEdit)sub_c).Text.Trim()))
                            {
                                if (required_list.ContainsKey(((DevExpress.XtraEditors.TextEdit)sub_c).Name))
                                {
                                    error = "Plaase enter " + required_list[((DevExpress.XtraEditors.TextEdit)sub_c).Name];
                                }
                            }
                        }

                        if (sub_c is DevExpress.XtraEditors.ComboBoxEdit)
                        {
                            if (((DevExpress.XtraEditors.ComboBoxEdit)sub_c).SelectedIndex < 0)
                            {
                                if (required_list.ContainsKey(((DevExpress.XtraEditors.ComboBoxEdit)sub_c).Name))
                                {
                                    error = "Please select " + required_list[((DevExpress.XtraEditors.ComboBoxEdit)sub_c).Name];
                                }
                            }
                        }
                    }
                }
            }            

            if (string.IsNullOrEmpty(error))
            {                   
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "UPDATE [231_RV201F06] SET"
                        + " [Serial No(RJ)]='" + this.serial_No_RJ.Text.Trim() + "',"
                        + " [Comp No#]='" + this.partNo.Text.Trim() + "',"
                        + " [Serial No(RRC)]='" + this.serialNo.Text.Trim() + "',"
                        + " [Written(Name)]='" + this.writtenName.Text.Trim() + "',"
                        + " [Written(No#)]='" + this.writtenID.Text.Trim() + "',"
                        + " [Checked(Name)]='" + this.checkName.Text.Trim() + "',"
                        + " [Checked(No#)]='" + this.checkID.Text.Trim() + "',"
                        + " [Written Year]='" + this.writtenYear.Text.Trim() + "',"
                        + " [Written Month]='" + this.writtenMonth.Text.Trim() + "',"
                        + " [Written Day]='" + this.writtenDay.Text.Trim() + "',"
                        + " [Export Year]='" + this.exportYear.Text.Trim() + "',"
                        + " [Export Month]='" + this.exportMonth.Text.Trim() + "',"
                        + " [Export Day]='" + this.exportDay.Text.Trim() + "',"
                        + " [Customer]='" + this.customerID.Text.Trim() + "',"
                        + " [Remark]='" + this.Remark.Text.Trim() + "', [Appearance Diagram]='" + this.appDiagram.Text.Trim() + "',"
                        + " [Wiring Diagram]='" + this.wiringDiagram.Text.Trim() + "', [RC-510 Serial]=NULLIF('" + this.RC_510_Serial.Text.Trim() + "', ''),"
                        + " [SB020 Serial]=NULLIF('" + this.SB020_Serial.Text.Trim() + "', ''), [SB017A Serial]=NULLIF('" + this.SB017A_Serial.Text.Trim() + "', ''),"
                        + " [Indicator1 Serial]=NULLIF('" + this.Indicator1_Serial.Text.Trim() + "', ''),"
                        + " [SB019(Door) Serial]=NULLIF('" + this.SB019_Door_Serial.Text.Trim() + "', ''),"
                        + " [SB019(Stg1) Serial]=NULLIF('" + this.SB019_Stg1_Serial.Text.Trim() + "', ''),"
                        + " [SB019(Stg2) Serial]=NULLIF('" + this.SB019_Stg2_Serial.Text.Trim() + "', ''),"
                        + " [Y-Axis Driver Serial]=NULLIF('" + this.Y_Axis_Driver_Serial.Text.Trim() + "', ''),"
                        + " [Z-Axis Driver Serial]=NULLIF('" + this.Z_Axis_Driver_Serial.Text.Trim() + "', ''),"
                        + " [CID Serial]=NULLIF('" + this.CID_Serial.Text.Trim() + "', ''),"
                        + " [RC-510 Program File]=NULLIF('" + this.RC_510_Program_File.Text.Trim() + "', ''),"
                        + " [Program Version]=NULLIF('" + this.Program_Version.Text.Trim() + "', ''),"
                        + " [FPGA Version]=NULLIF('" + this.FPGA_Version.Text.Trim() + "', ''),"
                        + " [Initialize Data File]=NULLIF('" + this.Initialize_Data_File.Text.Trim() + "', ''),"
                        + " [Body No]=NULLIF('" + this.Body_No.Text.Trim() + "', ''),"
                        + " [IP Adress]=NULLIF('" + this.IP_Address.Text.Trim() + "', ''),"
                        + " [MAC Address 1]=NULLIF('" + this.MAC_Address1.Text.Trim() + "', ''),"
                        + " [MAC Address 2]=NULLIF('" + this.MAC_Address2.Text.Trim() + "', ''),"
                        + " [MAC Address 3]=NULLIF('" + this.MAC_Address3.Text.Trim() + "', ''),"
                        + " [Y-Axis Position 1]=NULLIF('" + this.Y_Position1.Text.Trim() + "', ''),"
                        + " [Y-Axis Position 2]=NULLIF('" + this.Y_Position2.Text.Trim() + "', ''),"
                        + " [Y-Axis Position 3]=NULLIF('" + this.Y_Position3.Text.Trim() + "', ''),"
                        + " [Y-Axis Position 4]=NULLIF('" + this.Y_Position4.Text.Trim() + "', ''),"
                        + " [Y-Axis Position 5]=NULLIF('" + this.Y_Position5.Text.Trim() + "', ''),"
                        + " [Y-Axis Position 6]=NULLIF('" + this.Y_Position6.Text.Trim() + "', ''),"
                        + " [Z-Axis Position 1]=NULLIF('" + this.Z_Position1.Text.Trim() + "', ''),"
                        + " [Z-Axis Position 2]=NULLIF('" + this.Z_Position2.Text.Trim() + "', ''),"
                        + " [Z-Axis Position 3]=NULLIF('" + this.Z_Position3.Text.Trim() + "', ''),"
                        + " [Z-Axis Position 4]=NULLIF('" + this.Z_Position4.Text.Trim() + "', ''),"
                        + " [Z-Axis Position 5]=NULLIF('" + this.Z_Position5.Text.Trim() + "', ''),"
                        + " [Z-Axis Position 6]=NULLIF('" + this.Z_Position6.Text.Trim() + "', ''),"
                        + " [Mapping Top]=NULLIF('" + this.Mapping_Top.Text.Trim() + "', ''),"
                        + " [Mapping Bottom]=NULLIF('" + this.Mapping_Bottom.Text.Trim() + "', ''),"
                        + " [BCR Reading]=NULLIF('" + this.BRC_Reading.Text.Trim() + "', ''),"
                        + " [Stacker Hand Threshold]=NULLIF('" + this.Stacker_Hand_Threshold.Text.Trim() + "', ''),"
                        + " [Laser Prt# Threshold]=NULLIF('" + this.Laser_Print_Threshold.Text.Trim() + "', ''),"
                        + " [YAX1 Driver Ver]=NULLIF('" + this.YAX1_Driver_Ver.Text.Trim() + "', ''),"
                        + " [ZAX1 Driver Ver]=NULLIF('" + this.ZAX1_Driver_Ver.Text.Trim() + "', ''),"
                        + " [RIO/Door Ver 1]=NULLIF('" + this.RIO_Door_Ver1.Text.Trim() + "', ''),"
                        + " [RIO/Stage Ver 1]=NULLIF('" + this.RIO_Stage_Ver1.Text.Trim() + "', ''),"
                        + " [RIO/Stage Ver 2]=NULLIF('" + this.RIO_Stage_Ver2.Text.Trim() + "', ''),"
                        + " [RIO/Indicator1 Ver 1]=NULLIF('" + this.RIO_Indicator1_Ver1.Text.Trim() + "', ''),"
                        + " [RIO/Indicator1 Ver 2]=NULLIF('" + this.RIO_Indicator1_Ver2.Text.Trim() + "', ''),"
                        + " [CPLD0 Ver]=NULLIF('" + this.CPLD0_Ver.Text.Trim() + "', ''),"
                        + " [CPLD1 Ver]=NULLIF('" + this.CPLD1_Ver.Text.Trim() + "', ''),"
                        + " [Loader LD-510 Ver]=NULLIF('" + this.LoaderLD_510_Ver.Text.Trim() + "', '')"
                        + " WHERE [Serial No(RRC)] = '" + this.serial + "'";
                    sql_cmd.ExecuteNonQuery();
                    sql_cmd.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Database updated successfully!");
                    this.Dispose();
                }
                catch (Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show(ex.Message);
                }                
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        private void writtenBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.writtenBy.SelectedIndex >= 0)
            {
                this.writtenName.Text = this.writtenBy.Text.Substring(0, this.writtenBy.Text.Length - 4);
                this.writtenID.Text = this.writtenBy.Text.Substring(this.writtenBy.Text.Length - 4, 4);
            }
        }

        private void checkBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.checkBy.SelectedIndex >= 0)
            {
                this.checkName.Text = this.checkBy.Text.Substring(0, this.checkBy.Text.Length - 4);
                this.checkID.Text = this.checkBy.Text.Substring(this.checkBy.Text.Length - 4, 4);
            }
        }

        private void writtenDate_EditValueChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.writtenDate.EditValue.ToString()))
            {
                this.writtenYear.Text = DateTime.Parse(this.writtenDate.EditValue.ToString()).Year.ToString("D4");
                this.writtenMonth.Text = DateTime.Parse(this.writtenDate.EditValue.ToString()).Month.ToString("D2");
                this.writtenDay.Text = DateTime.Parse(this.writtenDate.EditValue.ToString()).Day.ToString("D2");
            }
        }

        private void exportDate_EditValueChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.exportDate.EditValue.ToString()))
            {
                this.exportYear.Text = DateTime.Parse(this.exportDate.EditValue.ToString()).Year.ToString("D4");
                this.exportMonth.Text = DateTime.Parse(this.exportDate.EditValue.ToString()).Month.ToString("D2");
                this.exportDay.Text = DateTime.Parse(this.exportDate.EditValue.ToString()).Day.ToString("D2");
            }
        }

        private void serialNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.serialNo.Text.Substring(this.serialNo.Text.Length - 1, 1).ToUpper() == "F")
            {
                this.serial_No_RJ.Text = this.serialNo.Text.Substring(0, this.serialNo.Text.Length - 1);
            }
            else
            {
                this.serial_No_RJ.Text = this.serialNo.Text;
            }
        }

        private void RC_510_Serial_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.RC_510_Serial.SelectedIndex >= 0)
            {
                if (this.RC_510_Serial.Text.Length >= 7)
                {
                    this.MAC_Address1.Text = this.RC_510_Serial.Text.Substring(1, 2);
                    this.MAC_Address2.Text = this.RC_510_Serial.Text.Substring(3, 2);
                    this.MAC_Address3.Text = this.RC_510_Serial.Text.Substring(5, 2);
                }
            }
        }

        private void serialNo_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (this.serialNo.Text.Substring(this.serialNo.Text.Length - 1, 1).ToUpper() == "F")
            {
                this.serial_No_RJ.Text = this.serialNo.Text.Substring(0, this.serialNo.Text.Length - 1);
            }
            else
            {
                this.serial_No_RJ.Text = this.serialNo.Text;
            }
        }
    }
}
